package com.hq.ims.data.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hq.ims.data.config.verification.VdAdd;
import com.hq.ims.data.dao.UserTodoStatisticsDetailMapper;
import com.hq.ims.data.entity.UserTodoStatisticsDetail;
import com.hq.ims.data.entityenum.EnumUserTodoType;
import com.hq.ims.data.vo.UserTodoStatisticsDetailMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.utils.util.ListUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 蒋柏平
 * @detail 用户端待办统计详情 服务实现类
 * @since 20241108
 */
@Service
public class BaseUserTodoStatisticsDetailService extends BaseService<UserTodoStatisticsDetailMapper, UserTodoStatisticsDetail> {


    /**
     * 分页
     */
    public List<UserTodoStatisticsDetailMapstruct.UserTodoStatisticsDetailVo> getByPage(
            RequestPage page,
            UserTodoStatisticsDetailMapstruct.PageSearchVo searchVo
    ) {
        //验证
        LambdaQueryChainWrapper<UserTodoStatisticsDetail> lq = this.lambdaQuery();
        //排序
        lq.orderByDesc(UserTodoStatisticsDetail::getCreateDateTime);
        //查询条件

        //执行分页
        lq.page(page);

        List<UserTodoStatisticsDetail> dataList = page.getRecords();
        List<UserTodoStatisticsDetailMapstruct.UserTodoStatisticsDetailVo> voList = UserTodoStatisticsDetailMapstruct.INSTANCE.toUserTodoStatisticsDetailVoList(dataList);
        page.setRecords(voList);
        return voList;
    }

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    public UserTodoStatisticsDetail addData(UserTodoStatisticsDetailMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdAdd.class);
        UserTodoStatisticsDetail entity = this.getByUserTodoTypeAndTableId(vo.getUserTodoType(), vo.getTableId(), vo.getUserId(), vo.getCompanyId());
        if (entity != null) {
            return entity;
        }

        //账值新的对象
        entity = new UserTodoStatisticsDetail();
        entity.setUserTodoStatisticsId(vo.getUserTodoStatisticsId());
        entity.setUserTodoType(vo.getUserTodoType());
        entity.setTableId(vo.getTableId());

        entity.insert();
        return entity;
    }

    public UserTodoStatisticsDetailMapstruct.UserTodoStatisticsDetailVo getByUserTodoTypeAndTableId(
            EnumUserTodoType userTodoType,
            String dataId
    ) {
        List<UserTodoStatisticsDetail> list = this.lambdaQuery()
                .eq(UserTodoStatisticsDetail::getUserTodoType, userTodoType)
                .eq(UserTodoStatisticsDetail::getTableId, dataId)
                .list();
        if (ListUtils.isExistsEle(list)) {
            return UserTodoStatisticsDetailMapstruct.INSTANCE.toUserTodoStatisticsDetailVo(list.get(0));
        }
        return null;
    }

    public UserTodoStatisticsDetailMapstruct.UserTodoStatisticsDetailVo getByUserTodoTypeAndTableId(
            EnumUserTodoType userTodoType,
            String dataId,
            String userId,
            String companyId
    ) {
        List<UserTodoStatisticsDetail> list = this.lambdaQuery()
                .eq(UserTodoStatisticsDetail::getUserTodoType, userTodoType)
                .eq(UserTodoStatisticsDetail::getTableId, dataId)
                .eq(UserTodoStatisticsDetail::getUserId, userId)
                .eq(UserTodoStatisticsDetail::getCompanyId, companyId)
                .list();
        if (ListUtils.isExistsEle(list)) {
            return UserTodoStatisticsDetailMapstruct.INSTANCE.toUserTodoStatisticsDetailVo(list.get(0));
        }
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean isExistsByPlatformTodoTypeAndTableId(
            EnumUserTodoType userTodoType,
            String dataId
    ) {
        Long count = this.lambdaQuery()
                .eq(UserTodoStatisticsDetail::getUserTodoType, userTodoType)
                .eq(UserTodoStatisticsDetail::getTableId, dataId)
                .count();
        return count > 0;
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void delData(String userTodoStatisticsDetailId) {
        UserTodoStatisticsDetail data = this.getById(userTodoStatisticsDetailId);
        if (data == null) {
            return;
        }
        data.deleteById();
    }

    /**
     * 根据平台待办类型和表ID删除
     *
     * @param userTodoType 用户待办类型
     * @param dataId       数据ID
     */
    @Transactional(rollbackFor = Exception.class)
    public void delByUserTodoTypeAndTableId(
            @NotNull EnumUserTodoType userTodoType,
            @NotNull String dataId,
            @NotNull String userId,
            @NotNull String companyId
    ) {
        this.lambdaUpdate()
                .eq(UserTodoStatisticsDetail::getUserTodoType, userTodoType)
                .eq(UserTodoStatisticsDetail::getTableId, dataId)
                .eq(UserTodoStatisticsDetail::getUserId, userId)
                .eq(UserTodoStatisticsDetail::getCompanyId, companyId)
                .remove();
    }

    /**
     * 根据平台待办类型和表ID删除
     *
     * @param userTodoType 用户待办类型
     * @param dataId       数据ID
     */
    @Transactional(rollbackFor = Exception.class)
    public void delByUserTodoTypeAndTableId(
            EnumUserTodoType userTodoType,
            String dataId
    ) {
        this.lambdaUpdate()
                .eq(UserTodoStatisticsDetail::getUserTodoType, userTodoType)
                .eq(UserTodoStatisticsDetail::getTableId, dataId)
                .remove();
    }

    /**
     * 获取原始数据详情
     */
    public UserTodoStatisticsDetailMapstruct.UserTodoStatisticsDetailVo getDetailVo(String userTodoStatisticsDetailId) {
        UserTodoStatisticsDetail entity = this.getById(userTodoStatisticsDetailId);
        UserTodoStatisticsDetailMapstruct.UserTodoStatisticsDetailVo vo = UserTodoStatisticsDetailMapstruct.INSTANCE.toUserTodoStatisticsDetailVo(entity);
        return vo;
    }
}